package com.qc31.comlib.choose.car

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.utils.*
import com.qc31.comlib.Keys
import com.qc31.comlib.R
import com.qc31.comlib.databinding.FragmentChooseCarBinding
import com.qc31.comlib.databinding.IncludeChooseTeamCarBinding
import com.qc31.comlib.entity.ChooseTeamCar

/**
 *@Author Jasper
 *@Time   2020/7/22
 *@Desc   选择车辆
 */
class ChooseCarFragment(private val isVideo: Boolean = false) :
    LazyFragment<FragmentChooseCarBinding>(FragmentChooseCarBinding::inflate) {
    private lateinit var mChooseTeamVB: IncludeChooseTeamCarBinding

    //是否添加数据
    private var isAdd = true


    //空布局
    private val emptyView by lazy {
        val view=    layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.includeRecycler.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(),
            R.mipmap.ic_empty)
        view
    }

    private val mViewModel: ChooseCarViewModel by viewModels()


    override fun initView(view: View) {
        mChooseTeamVB = IncludeChooseTeamCarBinding.bind(binding.root)
        mViewModel.rootName = getString(R.string.desc_choose_root)
        mViewModel.init()
        initRecycler()
    }

    override fun initData() {
        mViewModel.isVideo = isVideo
        mViewModel.getTeamCars()

        mViewModel.titleName.hide().autoDispose(scopeProvider).subscribe {
            mChooseTeamVB.tvTeamName.text = it
        }

        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
    }

    override fun initEvent() {
        mChooseTeamVB.tvTeamBack.setOnClickListener {
            mViewModel.getPreviousTeam()
        }
        mChooseTeamVB.tvTeamHome.setOnClickListener {
            mViewModel.getHomeTeam()
        }
    }

    //初始化
    private fun initRecycler() {
        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(context)
        binding.includeRecycler.recyclerView.adapter = ChooseCarAdapter().apply {
            setOnItemClickListener { _, _, position ->
                val entity = data[position]
                if (entity.carId.isNotEmpty()) {
                    entity.run {
                        setResultTo(requireActivity()) {
                            putExtra(Keys.INTENT_CAR_ID, carId)
                            putExtra(Keys.INTENT_CAR_NAME, subPlate(carPlate, 14))
                            if (videos != null) putExtra(Keys.VIDEO, videos)
                        }
                        ChooseCars.addCars(
                            carId = carId,
                            carPlate = carPlate,
                            carName = carName,
                            videos = videos,
                            iconLink = iconLink,
                            isVideo = !videos.isNullOrEmpty()
                        )
                    }
                    requireActivity().finish()
                } else {
                    if (entity.carNum == 0) {
                        R.string.toast_choose_no_car.toast(requireContext())
                        return@setOnItemClickListener
                    }
                    mViewModel.getNextTeam(entity.teamId, entity.teamName)
                    recyclerView.scrollToPosition(0)
                }
            }

            mViewModel.listObserver().autoDispose(scopeProvider).subscribe {
                if (it.isEmpty()) {
                    setEmptyView(emptyView)
                } else {
                    if (isAdd) {
                        isAdd = false
                        setNewInstance(it)
                    } else {
                        notifyDataSetChanged()
                    }
                }
            }

        }
    }

    private val acc by lazy { getString(R.string.desc_ACC) }

    inner class ChooseCarAdapter :
        BaseQuickAdapter<ChooseTeamCar, BaseViewHolder>(R.layout.item_choose_car) {
        override fun convert(holder: BaseViewHolder, item: ChooseTeamCar) {
            val tvItemChoose = holder.getView<TextView>(R.id.tvItemChoose)
            tvItemChoose.visibility = View.GONE
            item.run {
                holder.let {
                    if (carId.isEmpty()) {
                        it.setText(
                            R.id.tvItemName,
                            "${sub(teamName, 11)}  ($carNum)"
                        )
                        it.setImageResource(R.id.ivItemPic, R.mipmap.ic_team_pic)
                    } else {
                        var video = ""
                        if (isVideo) {
                            video = "(${context.getString(R.string.desc_choose_video)})"
                        }
                        it.setText(
                            R.id.tvItemName,
                            "${
                                sub(
                                    if (carName == carPlate) carName else "$carPlate($carName)",
                                    20
                                )
                            }[${acc} $accState]$video"
                        )
                        it.getView<ImageView>(R.id.ivItemPic)
                            .loadStringDImgError(context, iconLink, R.mipmap.ic_car_location_no)
                    }
                }
            }
        }
    }
}